<!-- 师昌绪初评header -->
<template>
  <ContentWrap v-loading="infoLoading">
    <el-descriptions :title="t('judgeScene.card.title')" :column="8" direction="vertical" border>
      <el-descriptions-item :label="t('judgeScene.card.awardsName')">
        {{ awardsInfo.awardsName }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('judgeScene.card.awards')">
        {{ awardsInfo.templateName }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('judgeScene.card.stage')">
        {{ getJudgeStatus }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('judgeScene.card.studentNum')">
        <p
          >{{ t('instance.fields.total') }}:
          {{ (awardsInfo.masterCount ?? 0) + (awardsInfo.doctorCount ?? 0) }}</p
        >
        <p>{{ t('instance.fields.masterTotal') }}: {{ awardsInfo.masterCount }}</p>
        <p>{{ t('instance.fields.doctorTotal') }}: {{ awardsInfo.doctorCount }}</p>
      </el-descriptions-item>
      <el-descriptions-item label="材料物理与化学">
        <div v-if="major1">
          <div>{{ t('judgeScene.card.judgeTime') }}</div>
          <div>
            {{ formatDate(major1.reviewStartTime, 'YYYY-MM-DD') }}
            ~
            {{ formatDate(major1.reviewEndTime, 'YYYY-MM-DD') }}
          </div>
        </div>
        <div v-else>{{ t('judgeScene.card.unset') }}</div>
        <el-link
          class="mr-[10px]"
          type="primary"
          v-if="major1"
          :underline="false"
          @click="handleShowJudge(major1.id)"
        >
          {{ t('judgeScene.card.viewJudge') }}
        </el-link>
        <el-link type="primary" :underline="false" @click="handleEditMajor(1, major1)">
          {{ t('judgeScene.card.setup') }}
        </el-link>
      </el-descriptions-item>
      <el-descriptions-item label="材料学">
        <div v-if="major2">
          <div>{{ t('judgeScene.card.judgeTime') }}</div>
          <div>
            {{ formatDate(major2.reviewStartTime, 'YYYY-MM-DD') }}
            ~
            {{ formatDate(major2.reviewEndTime, 'YYYY-MM-DD') }}
          </div>
        </div>
        <div v-else>{{ t('judgeScene.card.unset') }}</div>
        <el-link
          class="mr-[10px]"
          type="primary"
          v-if="major2"
          :underline="false"
          @click="handleShowJudge(major2.id)"
        >
          {{ t('judgeScene.card.viewJudge') }}
        </el-link>
        <el-link type="primary" :underline="false" @click="handleEditMajor(2, major2)">
          {{ t('judgeScene.card.setup') }}
        </el-link>
      </el-descriptions-item>
      <el-descriptions-item label="材料加工工程">
        <div v-if="major3">
          <div>{{ t('judgeScene.card.judgeTime') }}</div>
          <div>
            {{ formatDate(major3.reviewStartTime, 'YYYY-MM-DD') }}
            ~
            {{ formatDate(major3.reviewEndTime, 'YYYY-MM-DD') }}
          </div>
        </div>
        <div v-else>{{ t('judgeScene.card.unset') }}</div>
        <el-link
          class="mr-[10px]"
          type="primary"
          v-if="major3"
          :underline="false"
          @click="handleShowJudge(major3.id)"
        >
          {{ t('judgeScene.card.viewJudge') }}
        </el-link>
        <el-link type="primary" :underline="false" @click="handleEditMajor(3, major3)">
          {{ t('judgeScene.card.setup') }}
        </el-link>
      </el-descriptions-item>
      <el-descriptions-item label="腐蚀科学与防护">
        <div v-if="major4">
          <div>{{ t('judgeScene.card.judgeTime') }}</div>
          <div>
            {{ formatDate(major4.reviewStartTime, 'YYYY-MM-DD') }}
            ~
            {{ formatDate(major4.reviewEndTime, 'YYYY-MM-DD') }}
          </div>
        </div>
        <div v-else>{{ t('judgeScene.card.unset') }}</div>
        <el-link
          class="mr-[10px]"
          type="primary"
          v-if="major4"
          :underline="false"
          @click="handleShowJudge(major4.id)"
        >
          {{ t('judgeScene.card.viewJudge') }}
        </el-link>
        <el-link type="primary" :underline="false" @click="handleEditMajor(4, major4)">
          {{ t('judgeScene.card.setup') }}
        </el-link>
      </el-descriptions-item>
    </el-descriptions>
  </ContentWrap>

  <!-- 初评设置 -->
  <ScxInitialMajorForm ref="scxInitialMajorFormRef" @success="getInfo" />
  <ScxInitialMajorJudgeSelect ref="scxInitialMajorJudgeSelectRef" />
</template>

<script setup lang="ts">
import { formatDate } from '@/utils/formatTime'
import ScxInitialMajorForm from '@/views/awards/scxinitialmajor/ScxInitialMajorForm.vue'
import ScxInitialMajorJudgeSelect from '@/views/awards/scxinitialmajorjudge/components/ScxInitialMajorJudgeSelect.vue'
import { InstanceApi, InstanceVO } from '@/api/awards/instance'
import { ScxInitialMajorApi, ScxInitialMajorVO } from '@/api/awards/scxinitialmajor'

const route = useRoute()
const router = useRouter() // 路由对象
const { t } = useI18n() // 国际化

const awardsInfo = ref<InstanceVO>({} as InstanceVO)
const majorInfo = ref<ScxInitialMajorVO[]>([])

const getJudgeStatus = computed(() => {
  // 1=未开始 2=初评进行中 3=初评结束 4=终评进行中 5=终评结束
  switch (awardsInfo.value.judgeStatus) {
    case 1:
      return t('judgeScene.card.notStart')
    case 2:
      return t('judgeScene.card.initialUnderway')
    case 3:
      return t('judgeScene.card.initialEnd')
    case 4:
      return t('judgeScene.card.finalUnderway')
    case 5:
      return t('judgeScene.card.finalEnd')
    default:
      return t('judgeScene.card.notStart')
  }
})

const findMajor = (type: number) => {
  return majorInfo.value.find((item) => item.majorType === type)
}

const major1 = ref<ScxInitialMajorVO>()
const major2 = ref<ScxInitialMajorVO>()
const major3 = ref<ScxInitialMajorVO>()
const major4 = ref<ScxInitialMajorVO>()

const infoLoading = ref(false)
const getInfo = async () => {
  infoLoading.value = true
  const [awardsInfoRes, majorInfoRes] = await Promise.all([
    // 读取评奖基本信息
    InstanceApi.getInstance(awardsId.value),
    // 读取专业设置信息
    ScxInitialMajorApi.getScxInitialMajorListByInstance(awardsId.value)
  ])
  awardsInfo.value = awardsInfoRes
  majorInfo.value = majorInfoRes
  major1.value = findMajor(1)
  major2.value = findMajor(2)
  major3.value = findMajor(3)
  major4.value = findMajor(4)
  infoLoading.value = false
}

const scxInitialMajorJudgeSelectRef = ref<InstanceType<typeof ScxInitialMajorJudgeSelect>>()
const handleShowJudge = (id: string) => {
  scxInitialMajorJudgeSelectRef.value?.open(awardsId.value, id)
}

const scxInitialMajorFormRef = ref<InstanceType<typeof ScxInitialMajorForm>>()
const handleEditMajor = (type: number, major?: ScxInitialMajorVO) => {
  scxInitialMajorFormRef.value?.open(awardsId.value, type, major)
}

const awardsId = ref('')
/** 初始化 **/
onMounted(() => {
  awardsId.value = route.params.instanceId as string
  // 查询评审进行信息
  getInfo()
})
</script>
